﻿using NUnit.Framework;
using Selenium;
using Rhino.Mocks;
using SeleniumToolkit.Config;

namespace SeleniumToolkit.Core
{
    public class SeleniumFactorySpecs : SpecFor<SeleniumFactory>
    {
        ISeleniumFactoryProvider provider;
        SeleniumConfiguration config;
        ISelenium selenium;

        protected override void Context()
        {
            config = new SeleniumConfiguration();
            provider = config.ResolveFactory();
        }

        public override SeleniumFactory InitializeSubject()
        {
            return new SeleniumFactory(config, provider);
        }

        [TestFixture]
        public class WhenUsingCustomFactoryProvider : SeleniumFactorySpecs
        {
            protected override void Context()
            {
                base.Context();
                provider = MockRepository.GenerateMock<ISeleniumFactoryProvider>();
                provider.Expect(p => p.Create("localhost", 4444, "*iexplore", "http://www.google.com")).Return(new DefaultSelenium(null));
            }

            protected override void Because()
            {
                selenium = subject.CreateInstance();
            }

            [Test]
            public void ShouldBeAbleToCreatesInstance()
            {
                selenium.ShouldNotBeNull();
            }
        }

        [TestFixture]
        public class WhenUsingDefaultConfiguration : SeleniumFactorySpecs
        {
            [Test]
            public void ShouldHaveBrowserMappingSettings()
            {
                subject.BrowserMap
                            .ShouldNotBeNull()
                            .Count.ShouldBeGreaterThan(0);
            }

            [Test]
            public void ShouldBeAbleToCreateSeleniumInstances()
            {
                subject.CreateInstance().ShouldNotBeNull();
            }

            [Test]
            public void ShouldBeAbleToCreateInternetExplorerInstance()
            {
                subject.CreateInstance("*iexplore").ShouldNotBeNull();
            }

            [Test]
            public void ShouldBeAbleToCreateFirefoxInstance()
            {
                subject.CreateInstance("*firefox").ShouldNotBeNull();
            }
        }

        [TestFixture]
        public class WhenUsingCustomBrowserMap : SeleniumFactorySpecs
        {
            protected override void Because()
            {
                subject.BrowserMap.Add("ie", "*iexplore");
            }

            [Test]
            public void ShouldBeAbleToCreateSeleniumInstanceUsingAlias()
            {
                subject.CreateInstance("ie").ShouldNotBeNull();
            }

            [Test]
            public void ShouldNotBeAbleToCreateInstanceForNonExistentAlias()
            {
                exception = ((MethodThatThrows)(() =>
                {
                    subject.CreateInstance("foo");
                })).GetException();

                exception.ShouldNotBeNull();
            }
        }
    }
}
